<?php

namespace App\Models\ActivityRecord;

use App\Contracts\Repositories\ActivityRecordRepository;
use App\Models\Prize\Prize;
use App\Models\PrizeType\PrizeType;

class ActivityRecordService
{
    private $activityRecordRepository;

    public function __construct(ActivityRecordRepository $activityRecordRepository)
    {
        $this->activityRecordRepository = $activityRecordRepository;
    }

    public function create($activityRecord)
    {
        return $this->activityRecordRepository->create($activityRecord);
    }

    public function getActivityRecord($activityId){
        return $this->activityRecordRepository->getActivityRecord($activityId);
    }

    public function getActivityRecordsByIdOrTime($activity,$created_at = null){
        return $this->activityRecordRepository->getActivityRecordsByIdOrTime($activity,$created_at);
    }

    public function findByConditionsPage($condition, $sortArray = [], $start = 0, $limit = 10)
    {
        return $this->activityRecordRepository->findByConditionsPage($condition, $sortArray, $start, $limit);
    }

    public function count($conditions)
    {
        return (int)$this->activityRecordRepository->countByConditions($conditions);
    }

    public function countUserByConditions($conditions)
    {
        return (int)$this->activityRecordRepository->countUserByConditions($conditions);
    }

    public function update($updates, $id)
    {
        return $this->activityRecordRepository->updateRecord($updates, [['id', $id]]);
    }

    public function updateByConditions($updates, $conditions)
    {
        return $this->activityRecordRepository->updateRecord($updates, $conditions);
    }

    public function findOneByConditions($conditions, $sort)
    {
        return $this->activityRecordRepository->findByConditions($conditions, $sort)->first();
    }

    public function updateRecords($updates, $conditions)
    {
        return $this->activityRecordRepository->updateRecords($updates, $conditions);
    }

    public function exportData($activityId)
    {
        $conditions = [];
        $conditions[] = ['activity_id', $activityId];
        $list = $this->activityRecordRepository->findByConditionsPage($conditions, [], 0, 0);
        $exportData = [
            'activity' => null,
            'records'  => null,
        ];
        if ($list) {
            foreach ($list as $item) {
                $exportData['activity'] = $item->activity->name;
                $prize = $item->prize;
                $exportData['prizes'][$prize->id] = $prize->name;
                $logistics = $item->logistics;
                $prizeType = $prize->prizeType;
                $res = $this->exportDataBaseRow($item);
                if ($prizeType->code == PrizeType::ENTITY && $prize->exchange_method == Prize::MAIL_POST) {
                    $res['领奖人姓名'] = $item->name;
                    $res['领奖人手机号'] = $item->mobile;
                    $res['邮寄地址'] = $item->address;
                    $res['快递公司'] = '--';
                    $res['快递单号'] = '--';
                    if (!empty($logistics)) {
                        $res['快递公司'] = $logistics->company->name;
                        $res['快递单号'] = $logistics->order_number;
                    }
                }
                if($prize->extends){
                    $pFields = \GuzzleHttp\json_decode($prize->extends, true);
                    if (!empty($pFields)) {
                        foreach ($pFields as $key => $val) {
                            $res[$val] = '--';
                        }
                    }
                }
                if ($item->extends) {
                    $fields = \GuzzleHttp\json_decode($item->extends, true);
                    if ($fields) {
                        foreach ($fields as $k => $v) {
                            $res[$k] = $v;
                        }
                    }
                }
                $exportData['records'][$prize->id][] = $res;
            }
        }
        return $exportData;

    }

    private function exportDataBaseRow($activityRecord)
    {
        $prize = $activityRecord->prize;
        $prizeType = $prize->prizeType;
        $res = [];
        $res['中奖时间'] = $activityRecord->created_at;
        $res['中奖ID'] = $activityRecord->openid;
        $res['中奖人'] = is_null($activityRecord->participant) ? '--' : $activityRecord->participant->nickname;
        $res['中奖人手机号'] = is_null($activityRecord->participant) ? '--' : $activityRecord->participant->mobile;;
        $res['奖品名称'] = $prize->name;
        $res['奖品类型'] = $prizeType->name;
        $res['中奖状态'] = $activityRecord->is_exchange ? '已兑奖' : '未兑奖';
        $res['兑奖码'] = $activityRecord->coupon;


        return $res;
    }
}
